package org.dragon.controllers;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.dragon.commons.Const;
import org.dragon.commons.JsonResult;
import org.dragon.commons.Permit;
import org.dragon.models.AppConfig;
import org.dragon.models.SiteConfig;
import org.dragon.services.ConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Author:     Zhao Yan
 * DateTime:   2023/5/23 10:55
 */
@Validated
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Resource
    private ConfigService configService;

    @GetMapping("/site")
    public Object getSiteConfig() {
        AppConfig config = configService.getAppConfig();
        SiteConfig result = new SiteConfig();
        result.title = config.title;
        result.giteeLoginEnabled = config.giteeConfig.enabled;

        return result;
    }

    @Permit(roles = Const.ROOT)
    @GetMapping("/app")
    public Object getAppConfig() {
        return configService.getAppConfig();
    }

    @Permit(roles = Const.ROOT)
    @PostMapping("/app")
    public Object saveAppConfig(@RequestBody @Valid AppConfig config) {
        configService.saveAppConfig(config);
        return JsonResult.ok();
    }
}
